﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Yuans.BinaryConfig.OperationBoardControl
{
    public partial class NumberItemOperationBoard : UserControl
    {
        private long mNumberValue;
        public long NumberValue
        {
            get { return mNumberValue; }
            set { mNumberValue = value;
            numberInputBox.Text = value.ToString();
            }
        }
        public NumberItemOperationBoard()
        {
            InitializeComponent();
        }

        private void ConvertStringToNumber(string numberString)
        {
            if(Regex.Match(numberString,"0[xX][0-9a-fA-F]+").Success)
            {
                NumberValue = Int64.Parse(numberString.Substring(2), System.Globalization.NumberStyles.HexNumber);
            }
            else if(Regex.Match(numberString, "^-?[0-9]+").Success)
            {
                NumberValue = Int64.Parse(numberString, System.Globalization.NumberStyles.Integer);
            }
            else
            {
                numberInputBox.Clear();
            }
        }
        private void numberInputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ConvertStringToNumber(numberInputBox.Text);
            }
        }
        private void numberInputBox_Validated(object sender, EventArgs e)
        {
            ConvertStringToNumber(numberInputBox.Text);
        }
    }

}
